<template>
    <view class="container">
        <u--form labelPosition="left" labelWidth="90" :model="detail" :rules="rules" ref="uForm">
            <u-form-item label="客户名称" prop="nickname" borderBottom>
                <u--input v-model="detail.nickname" border="none" placeholder="请输入客户名称"></u--input>
            </u-form-item>
            <u-form-item label="邮箱" prop="email" borderBottom>
                <u--input v-model="detail.email" border="none" placeholder="请输入客户邮箱"></u--input>
            </u-form-item>
            <u-form-item label="手机号码" prop="mobile" borderBottom>
                <u--input v-model="detail.mobile" border="none" placeholder="请输入客户电话"></u--input>
            </u-form-item>
            <u-form-item label="密码" prop="password" borderBottom>
                <u--input type="password" v-model="password" border="none" placeholder="登录密码为空即不修改密码"></u--input>
            </u-form-item>
            <u-form-item
					label="性别"
					prop="sex"
					borderBottom
					@click="showSex = true; "
					ref="item1"
			>
				<u--input
						v-model="detail.sex_text"
						disabled
						disabledColor="#ffffff"
						placeholder="请选择性别"
						border="none"
				></u--input>
				<u-icon
						slot="right"
						name="arrow-right"
				></u-icon>
			</u-form-item>
            <u-form-item
					label="认证状态"
					prop="auth"
					borderBottom
					@click="showAuth = true; "
					ref="item1"
			>
				<u--input
						v-model="detail.auth_text"
						disabled
						disabledColor="#ffffff"
						placeholder="请选择性别"
						border="none"
				></u--input>
				<u-icon
						slot="right"
						name="arrow-right"
				></u-icon>
			</u-form-item>
            <u-form-item label="金额" prop="money" borderBottom>
                <u--input v-model="detail.money" border="none" placeholder="请输入客户金额"></u--input>
            </u-form-item>
            <u-form-item
					label="成交状态"
					prop="deal"
					borderBottom
					@click="showDeal = true; "
					ref="item1"
			>
				<u--input
						v-model="detail.deal_text"
						disabled
						disabledColor="#ffffff"
						placeholder="请选择成交状态"
						border="none"
				></u--input>
				<u-icon
						slot="right"
						name="arrow-right"
				></u-icon>
			</u-form-item>
            <u-form-item
					label="客户来源"
					prop="source"
					borderBottom
					@click="showSource = true; "
					ref="item1"
			>
				<u--input
						v-model="detail.source.name"
						disabled
						disabledColor="#ffffff"
						placeholder="请选择客户来源"
						border="none"
				></u--input>
				<u-icon
						slot="right"
						name="arrow-right"
				></u-icon>
			</u-form-item>

            <!-- 地区 -->
            <u-form-item label='选择地区' prop='region_text' @click="RegionShow = true">
                <pick-regions :defaultRegion="detail.district" @getRegion="handleGetRegion">
                    <u--input v-model="detail.region_text" disabled disabledColor="#ffffff" placeholder="请选择地区"
                        border="none">
                    </u--input>
                    <u-icon slot="right" name="arrow-right">
                    </u-icon>
                </pick-regions>
                <u-icon
                    slot="right"
                    name="arrow-right"
                >
                </u-icon>
            </u-form-item>

        </u--form>

        <u-action-sheet
				:show="showAuth"
				:actions="actions1"
				title="请选择认证状态"
				@close="showAuth = false"
				@select="authSelect"
		>
		</u-action-sheet>

        <u-action-sheet
				:show="showSex"
				:actions="actions"
				title="请选择性别"
				@close="showSex = false"
				@select="sexSelect"
		>
        </u-action-sheet>

        <u-action-sheet
				:show="showDeal"
				:actions="actions2"
				title="请选择成交状态"
				@close="showDeal = false"
				@select="dealSelect"
		>
		</u-action-sheet>

        <u-action-sheet
				:show="showSource"
				:actions="source"
				title="请选择客户来源"
				@close="showSource = false"
				@select="sourceSelect"
		>
		</u-action-sheet>
		
        <view style="margin-top: 25px;">
            <u-button type="warning" @click="submit">提交</u-button>
        </view>
        <u-toast ref="uToast"></u-toast>
    </view>
</template>

<script>
// 引用地区选择组件

import pickRegions from '@/components/pick-regions/pick-regions.vue'
export default {
    components:{
        pickRegions
    },
    data() {
        return {
            
            RegionShow: false,
            RegionCode: null,
            LoginUser:null,
            // form:{
            //     consignee:'',
            //     mobile:'',
            //     address:'',
            //     region_text:'',
            //     region:[]
            // },

            id:0,
            showSex: false,
            showAuth:false,
            showDeal:false,
            showSource:false,
            detail:{
                nickname:'',
                email:'',
                mobile:'',
                money:'',
            },
            source:[],
            password:'',

            rules:{
                nickname: [{
                    type: 'string',
                    required: true,
                    message: '请填写客户名',
                    trigger: ['blur', 'change']
                },],
                email: [{
                    type: 'email',
                    // required: true,
                    message: '请填正确的邮箱',
                    trigger: ['blur', 'change']
                }],
                mobile: [
                    {
                        // 自定义验证函数，见上说明
                        // validator: (rule, value, callback) => {
                        //     // 上面有说，返回true表示校验通过，返回false表示不通过
                        //     // uni.$u.test.mobile()就是返回true或者false的
                        //     if (value) {
                        //         return uni.$u.test.mobile(value);
                        //     } else {
                        //         return true
                        //     }
                        // },
                        // pattern: /^1[356789]\d{9}$/,
                        // transform(value) {
                        //     return String(value);
                        // },

                        required: true,
                        min:11,
                        max:11,
                        message: '手机号码不正确',
                        // 触发器可以同时用blur和change
                        // trigger: ['change', 'blur'],
                    }],
                money: [{
                    type: 'float',
                    required: true,
                    message: '请填正确的金额',
                    trigger: ['blur', 'change']
                }],
                
            },
            actions: [{
				name: '男',
                sex:1,
				},
				{
					name: '女',
                    sex:2,
				},
				{
					name: '保密',
                    sex:0,
				},
			],
            actions1: [{
				name: '未认证',
                auth:0,
				},
				{
					name: '已认证',
                    auth:1,
				}
			],
            actions2: [{
				name: '未成交',
                deal:0,
				},
				{
					name: '已成交',
                    deal:1,
				}
			],
            // business:{
            //     nickname:
            // },
            LoginAdmin:uni.getStorageSync('LoginAdmin') ? uni.getStorageSync('LoginAdmin') : {},
        }
    },
    onReady() {
		// 如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
    	// this.$refs.uForm.setRules(this.rules)
    },
    onLoad(options)
    {
        this.id = options.id;
        this.getDetail();
        // console.log(this.id);
    },
    methods: {
        handleGetRegion(item)
        {
            
            // console.log(11111);
            // 获取到选中的省，市，区
            let [province,city,district] = item

            // 定义字符串 => 存选中的地区文本
            let region_text = ''

            // 存选中的地区码
            let code = null

            let region = []

            if(province)
            {
                region_text += province.name + '-'
                region.push(province.name)
                this.detail.province = province.code
            }

            if(city)
            {
                region_text += city.name + '-'
                region.push(city.name)
                this.detail.city = city.code
            }

            if(district)
            {
                region_text += district.name
                region.push(district.name)
                this.detail.district = district.code
            }

            this.detail.region_text = region_text

            // this.form.region = region

            // this.RegionCode = code
            // console.log(this.form);
            // console.log(this.form.region);
            // console.log(this.RegionCode);
        },

        sexSelect(e) {
            // console.log(e);
			this.detail.sex_text = e.name
            this.detail.gender = e.sex;
            // console.log(this.detail.gender);
			// this.$refs.uForm.validateField('userInfo.sex')
		},

        authSelect(e) {
            // console.log(e);
			this.detail.auth_text = e.name
            this.detail.auth = e.auth;
            // console.log(this.detail.auth);
			// this.$refs.uForm.validateField('userInfo.sex')
		},

        dealSelect(e) {
            // console.log(e);
			this.detail.deal_text = e.name
            this.detail.deal = e.deal;
            // console.log(this.detail.deal);
			// this.$refs.uForm.validateField('userInfo.sex')
		},

        sourceSelect(e) {
            // console.log(e);
            this.detail.sourceid = e.id
			this.detail.source.name = e.name
            // this.detail.deal = e.deal;
            // console.log(this.detail.source.name);
			// this.$refs.uForm.validateField('userInfo.sex')
		},

        async getDetail()
        {
            let data = {
                    adminid:this.LoginAdmin.id,
                    
                    id:this.id
            }
            let result = await this.$u.api.manage.PrivateseaDetail(data);
            // console.log(result.data);
            this.detail = result.data;
            let result1 = await this.$u.api.manage.SourceIndex({adminid:this.LoginAdmin.id});
            this.source = result1.data;
            // this.detail.email = '';
            console.log(this.detail);
        },

        async submit()
        {
            // console.log(333);
            
  
                let data = {
                    adminid:this.LoginAdmin.id,
                    id:this.detail.id,
                    nickname:this.detail.nickname.trim(),
                    mobile:this.detail.mobile?this.detail.mobile.trim():'',
                    email:this.detail.email?this.detail.email.trim():'',
                    money:this.detail.money,
                    gender:this.detail.gender,
                    auth:this.detail.auth,
                    deal:this.detail.deal,
                    sourceid:this.detail.sourceid,
                    province:this.detail.province,
                    city:this.detail.city,
                    district:this.detail.district

                }

                if(this.password)
                {
                    // console.log(1111)
                    if(this.password.length < 6 || this.password.length > 14)
                    {
                        this.$refs.uToast.show({
                            type:'error',
                            message:'密码长度在6-14个字符之间'
                        });

                        return;
                    }

                    data.password = this.password.trim();
                }
                // console.log(2222)
                let result = await this.$u.api.manage.PrivateseaEdit(data);
                // return;
                if(result.code === 1)
                {
                    this.$refs.uToast.show({
                        type:'success',
                        message:result.msg,
                        complete:() => {
                            // uni.setStorageSync('LoginAdmin',result.data);

                            this.$u.route({
                                type:'navigateBack'
                            });

                            return;
                        }
                    })
                }else{
                    this.$refs.uToast.show({
                        type:'error',
                        message:result.msg
                    });

                    return;
                }

        }

    },
    onShow()
    {
        
        // console.log(this.id);
    },
}
</script>


<style>


.mobile{
    font-size: .8rem;
    color: gray;
    border-bottom: 2px solid rgb(170, 169, 169);
    margin-bottom: .5rem;
}

</style>